<?php
/**
 * Created by PhpStorm.
 * User: poptser
 * Date: 2017/7/21
 * Time: 15:07
 */

namespace SDXApi\Controller;


class LogisticsController extends AppController
{
    public function  get_order_logistics(){
        $order_id = I("post.order_id");
        $user_id = I("post.uid");
        $order_info = M("order_info")->where(array("order_id"=>$order_id,"user_id"=>$user_id))
            ->field("order_sn,order_status,shipping_status,pay_status,shipping_extend_type,shipping_exman_name,shipping_exman_mobile")->find();
        //returnApiError(M("order_info")->getLastSql());
        if(empty($order_info)){
            returnApiError("订单ID错误");
        }
        $data['order_sn'] = $order_info['order_sn'];
        $data['now'] = $this->Conversion_order($order_info['order_status'])."-".$this->Conversion_shipping($order_info['shipping_status']);
        $order_action = M("logistics")->where(array("order_id"=>$order_id))->order('log_time desc')->select();
        foreach ($order_action as $value){
            $data['rec'][] = array(
                "shipping_status"=>$this->Conversion_shipping($value['shipping_status'])." : ".$value['remark'],
                "remark"=>$value['remark'],
                "log_time"=>$value['log_time']+28800,
            );

        }
        returnApiSuccess($data);
    }


    public function  get_order_logistics_bak(){
        $order_id = I("post.order_id");
        $user_id = I("post.uid");
        $order_info = M("order_info")->where(array("order_id"=>$order_id,"user_id"=>$user_id))
            ->field("order_sn,order_status,shipping_status,pay_status")->find();
        //returnApiError(M("order_info")->getLastSql());
        if(empty($order_info)){
            returnApiError("订单ID错误");
        }
        $data['order_sn'] = $order_info['order_sn'];
        $data['now'] = $this->Conversion_order($order_info['order_status'])."-".$this->Conversion_shipping($order_info['shipping_status']);
        $order_action = M("order_action")->where(array("order_id"=>$order_id))->order('log_time desc')->select();
        foreach ($order_action as $value){

            if($value['shipping_status']==0){
                //未发货时，不显示备注
                $action_notes = explode("<",$value['action_note']);
                $action_note = explode("//",$action_notes[0]);
                if(empty($action_note[0])){
                    $action_note[0] = '后台已接收到您的订单，待配货';
                }
                $action_test = explode("已延迟配送",$action_notes[0]);

                $shipping_status = $this->Conversion_shipping($value['shipping_status'])." : ".$action_note[0].$action_test[1];
                //$shipping_status = $this->Conversion_shipping($value['shipping_status']);
            }elseif ($value['shipping_status']==1){
                //开始配送时显示配送员信息
                if(!empty($order_info['shipping_extend_type'])){
                    $shipping_exman_name =  $order_info['shipping_exman_name'];
                    $shipping_exman_mobile =  $order_info['shipping_exman_mobile'];
                }else{
                    $warehouse_personnel = M("warehouse_personnel")->where(array("uid"=>$value['user_id']))->find();
                    $shipping_exman_name =  $warehouse_personnel['name'];
                    $shipping_exman_mobile =  $warehouse_personnel['phone'];
                }

                $shipping_status = $this->Conversion_shipping($value['shipping_status'])." : 配送员:".$shipping_exman_name." 电话：".$shipping_exman_mobile;
            }elseif ($value['shipping_status']==2){
                //开始配送时显示配送员信息
                $shipping_status = $this->Conversion_shipping($value['shipping_status'])." : 您已签收订单，若商品有问题请在售后工单中提交申请，满意请给好评，将会有评价红包哦";
            }elseif ($value['shipping_status']==3){
                //开始配送时显示配送员信息
                $shipping_status = $this->Conversion_shipping($value['shipping_status'])." : 订单开始配货";
            }else{
                $action_notes = explode("<",$value['action_note']);
                $action_note = explode("//",$action_notes[0]);
                $action_test = explode("已延迟配送",$action_notes[0]);

                $shipping_status = $this->Conversion_shipping($value['shipping_status'])." : ".$action_note[0].$action_test[1];
            }

            if(!strstr($value['action_note'],'失败')&&!strstr($value['action_note'],'工单')&&!strstr($value['action_note'],'发票')&&!strstr($value['action_note'],'备注')){
                $data['rec'][] = array(
                    "shipping_status"=>$shipping_status,
                    "remark"=>$value['action_note'],
                    "log_time"=>$value['log_time']+28800,
                );
            }
        }
        returnApiSuccess($data);
    }


    public function Conversion_order($order_status = null){
        switch ($order_status){
            case 0:
                return '未确认';
                break;
            case 1:
                return '已确认';
                break;
            case 2:
                return '已取消';
                break;
            case 3:
                return '无效';
                break;
            case 4:
                return '退货';
                break;
            case 5:
                return '已分单';
                break;
            case 6:
                return '部分分单';
                break;
            default:
                return '未知';
        }
    }
    public function Conversion_shipping($shipping_status = null){
        switch ($shipping_status){
            case 0:
                return '未发货';
                break;
            case 1:
                return '配送中';
                break;
            case 2:
                return '已收货';
                break;
            case 3:
                return '配货中';
                break;
            case 4:
                return '已发货(部分)';
                break;
            case 5:
                return '已配货';
                break;
            case 6:
                return '代收货';
                break;
            default:
                return '未知';
        }
    }
}